
#ifndef Table_Lookup_JZ_h
#define Table_Lookup_JZ_h

#include <stdint.h>

/* alternate name */
#define Table_Lookup_JZ table_lookup_jz

/* size of biggest table we will use - max 255 */
#define MAX_ROWS 32
#define MAX_COLS 32

/* if you are using fixed axis spacing, then using these is more convenient */
#define min_x x_axis[0]
#define max_x x_axis[1]
#define min_y y_axis[0]
#define max_y y_axis[1]

#if 0   			/* for documentation only */
uint16_t rows;          	/* X (>= 1, 1 means 1D lookup) */
uint16_t cols;           	/* Y > 1 */
/* If you are not using variable axis, then these only hold the min then max values in the first two entries */
int16_t x_axis[MAX_ROWS];  	/* the x axis values - a little wasteful */
int16_t y_axis[MAX_COLS]; 	/* the y axis values, ignored for 1D lookup */
uint8_t variable_axis; 	/* true/false - 1 if variable, otherwise 0 */
uint8_t byte_table;		/* true/false - table data is 8 bit instead of 16 (slower but saves memory) */
uint8_t filler[2];		/* maintain 32 bit alignment */
#endif

/* same as above as a macro */
#define table_info 	\
	uint16_t rows;     \
	uint16_t cols; 	\
	int16_t x_axis[MAX_ROWS]; \
	int16_t y_axis[MAX_COLS];  \
	uint8_t variable_axis; \
	uint8_t byte_table;	\
	uint8_t filler[2]	

/* generic version */
struct table_jz
{
	table_info;
	uint8_t data[MAX_ROWS*MAX_COLS];   	/* rows*cols array of ints, X order, rows first */
};

int table_lookup_jz (const int x, const int y, const struct table_jz *t);
inline unsigned bsearch_jz (register const int value, const int16_t * array, unsigned n);
inline unsigned lsearch_jz (register const int value, const int16_t * array, unsigned n);

/* specific versions */
/* add more below as needed for other sizes or types */

/* 3x3 table with unsigned 8 bit values */
struct table_3x3_u8
{
	table_info;
	uint8_t data[3][3];   	/* rows*cols array of ints, X order, rows first */
};

/* 3x3 table with signed 16 bit values */
struct table_3x3_16
{
	table_info;
	int16_t data[3][3];   	/* rows*cols array of ints, X order, rows first */
};

/* 16x16 table with signed 16 bit values */
struct table_16x16_16
{
	table_info;
	int16_t data[16][16];   	/* rows*cols array of ints, X order, rows first */
};

/* Add more here...... */

#endif
